<?php
	
	namespace Aspic;
	
	/**
	* Standard class like PHP stdclass with special char to print value directly
	* ex: 
	* $inst->abc => returns abc
	* $inst->_abc => returns AND print directly abc
	*/
	Trait StdPrintTrait {
		use StdTrait {
			__construct as stdTraitInit;
		}
		
		protected $_printPrefix = '_';
		
		/**
		* @param string $printPrefix Determine a prefix that means:
		* "print the following field". 
		* Ex: $obj->fieldName returns the field value
		* $obj->_fieldName print (and return) the "fieldName" value if 
		* $printPrefix set to '_'
		*/
		public function __construct($vars = array(), $returnNullIfEmpty = true, $printPrefix = '_') {
			$this->stdTraitInit($vars, $returnNullIfEmpty);
			
			$this->_printPrefix = $printPrefix;
		}
		
		public function &__get($opt) {
			$parentVal = &parent::__get($opt);
			
			if(!isset($parentVal)) { // Parent could return a reference to innexistant var
				$fieldPossibleName = substr($opt, strpos($opt, $this->_printPrefix) + strlen($this->_printPrefix));
				
				if(isset($this->_vars[$fieldPossibleName])) {
					print $this->_vars[$fieldPossibleName];
					
					return $this->_vars[$fieldPossibleName];
				}
				else {
					return $parentVal; // null
				}
			}
			else {
				return $parentVal;
			}
		}
		
	}
	
?>